package chances.epg.commons.config;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class Config {
	private Map<String, Object> params;
	
	public Config(Map<String,Object> params){
		this.params = params;
	}
	/**
	 * 获取参数.
	 * 
	 * @param key
	 * @return
	 */
	public String getProperty(String key) {
		return this.getProperty(key, "");
	}

	/**
	 * 获取参数.
	 */
	public String getProperty(String key, String defaultValue) {
		if (this.params.containsKey(key)) {
			return this.params.get(key).toString();
		} else {
			return defaultValue;
		}

	}
	
	/**
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public boolean getBooleanValue(String key, boolean defaultValue) {
		String boolValue = this.getProperty(key);

		if (StringUtils.isBlank(boolValue)) {
			return defaultValue;
		}
		
		try {
			boolean result = Boolean.parseBoolean(boolValue);
			return result;
		} catch (Exception e) {
			throw new ConfigException(boolValue + " should be boolean value,invalid config key:" + key);
		}
	}

	/**
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public int getIntValue(String key, int defaultValue) {
		String intValue = this.getProperty(key);
		
		if (StringUtils.isBlank(intValue)) {
			return defaultValue;
		}
		
		try {
			int result = Integer.parseInt(intValue);
			return result;
		} catch (Exception e) {
			throw new ConfigException(intValue + " should be integer value,invalid config key:" + key);
		}
	}
}
